<?php

/*
 * 迪杰斯特拉算法
 * https://blog.csdn.net/baidu_22502417/article/details/40663213
 * https://blog.csdn.net/qq_35644234/article/details/60870719
 */

final class PathFinding {

    public $maze_width = 0;
    public $maze_height = 0;
    public $person_x = 0;
    public $person_y = 0;
    public $maze = [];
    public $export_x;
    public $export_y;

    /*
     * 5为人的出发点
     * 9为出口店
     * 0是可以走的路径点
     * 1是路障不可能穿越
     * 6行走的足迹
     * 注意：迷宫矩阵位置是例如10*10，但是数组是0-9，那么位置坐标就必须要减一
     */

    public function __construct($width = 10, $height = 10, $person_x = 1, $person_y = 1, $export_x = 10, $export_y = 10) {
        if ((int) $width <= 2) {
            throw new Exception('迷宫宽度不能小于等于10');
        }
        if ((int) $width <= 2) {
            throw new Exception('迷宫宽度不能小于等于2');
        }

        $this->maze_width = $width;
        $this->maze_height = $height;
        //人的坐标
        if ($person_x < 1 || $person_x > $this->maze_width) {
            throw new Exception('人的X坐标超出迷宫宽度');
        }
        if ($person_y < 1 || $person_y > $this->maze_height) {
            throw new Exception('人的Y坐标超出迷宫高度');
        }
        $this->person_x = $person_x;
        $this->person_y = $person_y;
        //出口的坐标
        if ($export_x < 1 || $export_x > $this->maze_width) {
            throw new Exception('出口的X坐标超出迷宫宽度');
        }
        if ($export_y < 1 || $export_y > $this->maze_height) {
            throw new Exception('出口的X坐标超出迷宫高度');
        }
        $this->export_x = $export_x;
        $this->export_y = $export_y;
        $this->ProductionMazeMatrix($this->maze_width, $this->maze_height);
        $this->maze[$person_x - 1][$person_y - 1] = 5;
        $this->maze[$export_x - 1][$export_y - 1] = 9;
    }

    //寻找迷宫出口
    public function SearchMazeExport() {
        //是否完成
        $Complete = false;
        //没有找到出口不算完成任务，默认一定有可以找到出口的路，而且有出口，复杂的以后再说
//        while ($Complete == false) {
//            
//        }
    }

    //深度优先
    public function Dfs() {
        
    }

    //最短路径
    public function Bfs() {
        
    }

    //生产迷宫矩阵
    protected function ProductionMazeMatrix($width, $height) {
        for ($x = 0; $x < $width; $x++) {
            for ($y = 0; $y < $height; $y++) {
                $this->maze[$x][$y] = 0;
            }
        }
    }

    //添加障碍 
    public function AddObstacles($x, $y, $type = 1) {
        if ($x < 1 || $x > $this->maze_width) {
            throw new Exception('设置的宽度超出迷宫宽度');
        }
        if ($y < 1 || $y > $this->maze_height) {
            throw new Exception('设置的高度超出迷宫高度');
        }
        if ($this->maze[$x - 1][$y - 1] == 1) {
            throw new Exception('此位置已经设置过障碍');
        }
        $this->maze[$x - 1][$y - 1] = (int) $type;
    }

    //打印迷宫矩阵
    public function PrintMazeMatrix() {
        p("迷宫宽度为{$this->maze_width},高度为{$this->maze_height}，打印出来如下");
        for ($x = 0; $x < $this->maze_width; $x++) {
            for ($y = 0; $y < $this->maze_height; $y++) {
                if ($this->maze[$x][$y] == 5) {
                    echo "<span style='color: red;'>{$this->maze[$x][$y]}</span>";
                } elseif ($this->maze[$x][$y] == 9) {
                    echo "<span style='color: #0924f9;'>{$this->maze[$x][$y]}</span>";
                } elseif ($this->maze[$x][$y] == 1) {
                    echo "<span style='color: red;'>{$this->maze[$x][$y]}</span>";
                } elseif ($this->maze[$x][$y] == 6) {
                    echo $this->maze[$x][$y];
                } else {
                    echo "<span style='color: #84c130;'>{$this->maze[$x][$y]}</span>";
                }
            }
            echo '<br>';
        }
        echo '<br>';
    }

}
